package cn.myjerry.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 学生成绩管理系统
 * @author Jerry
 *
 */

public class MapDemo03 {
	public static void main(String[] args) {
		List<Student> list = exam();
		Map<Integer, ClassRoom> map = sount(list);
		view(map);
	}
	
	public static void view(Map<Integer, ClassRoom> map) {
		Set<Integer> keySet = map.keySet();
		Iterator<Integer> keyIt = keySet.iterator();
		while (keyIt.hasNext()) {
			int no = keyIt.next();
			double total = map.get(no).getTotal();
			double svg = total/map.get(no).getStuList().size();
			System.out.println(no + "==>" + total + "==>" + svg);
		}
	}
	
	public static Map<Integer, ClassRoom> sount(List<Student> list) {
		Map<Integer, ClassRoom> map = new HashMap<>();
		for(Student stu:list) {
			int no = stu.getNo();
			double score = stu.getScore();
			ClassRoom room = map.get(no);
			if(null == room) {
				room = new ClassRoom(no);
				map.put(no, room);
			}
			room.getStuList().add(stu);
			room.setTotal(room.getTotal() + score);
		}
		return map;
	}
	
	public static List<Student> exam() {
		List<Student> list = new ArrayList<>();
		list.add(new Student("Jerry", 1, 80.5));
		list.add(new Student("Bobi", 2, 90));
		list.add(new Student("Tom", 1, 85));
		list.add(new Student("Kim", 2, 70));
		list.add(new Student("Davy", 1, 95));
		return list;
	}
}
